package com.luck.rabbit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.UUID;

/**
 * 发送消息类
 * Created by Hua wb on 2018/12/11.
 */
@Component
public class RabbitSend {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMessage(String content) {
        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_DEFAULT, RabbitConfig.ROUTING_KEY_DEFAULT, content,
                correlationId);
    }

    public void sendMessage(String content, String routingKey) {
        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_DEFAULT, routingKey, content,
                correlationId);
    }

    public void sendMessage(String content, int delayTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_DEFAULT, RabbitConfig.ROUTING_KEY_DELAY, content, message -> {
            message.getMessageProperties().setExpiration(delayTime + "");
            return message;
        });
    }

}

